package com.yjd.bi.statistic.control;

import java.util.List;
import java.util.Random;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.fund.model.BiFundsStatisticsModel;
import com.yjd.bi.fund.service.IBiFundsStatisticsMinuteService;
import com.yjd.bi.fund.service.IBiFundsStatisticsService;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.NumberUtil;

/**
 * <b>description</b>：系统管理员 <br>
 * <b>time</b>：2014-11-5下午5:01:04 <br>
 * <b>author</b>： ready likun_557@163.com
 */
@Controller
@Scope("prototype")
@RequestMapping("/funds/")
public class FundsControl extends BaseControl {
	
	@ServiceRemoteProxy(serviceUrlResouceKey=RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiFundsStatisticsService biFundsStatisticsService;
	
	@ServiceRemoteProxy(serviceUrlResouceKey=RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
	private IBiFundsStatisticsMinuteService biFundsStatisticsMinuteService;

	/**
	 * 管理员列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("index")
	public String deal() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("now", BiDateUtil.initBiDate());
		return "funds.index";
	}
	@RequestMapping("getSummary")
	public void getIndexData(String start,String end) throws Exception {
		try{
			start=getTime(start);
			end=getTime(end);
		}catch(Exception e){
			this.errorMsg("参数有误！");
		}
		BiFundsStatisticsModel model=this.biFundsStatisticsService.getSummaryModel(Long.parseLong(start), Long.parseLong(end));
		Double lckPrice = this.biFundsStatisticsService.getLckSummary(DateUtil.dateStringToTimestamp(start,DateUtil.PATTERN_yyyyMMdd), DateUtil.dateStringToTimestamp(end,DateUtil.PATTERN_yyyyMMdd)+86399);
		this.successData(FrameUtil.newHashMap("summaryVals",FrameUtil.newArrayList(NumberUtil.formatNumber(model.getVolume(), NumberUtil.FORMAT_4),
				NumberUtil.formatNumber(model.getRecharge(), NumberUtil.FORMAT_4),NumberUtil.formatNumber(model.getCash(), NumberUtil.FORMAT_4),
				NumberUtil.formatNumber(lckPrice, NumberUtil.FORMAT_4))));
	}
	
	public Double getRandomValue(){
		Random random = new Random();
		return random.nextDouble()*1000000;
	}
	
	/**
	 * 根据yyyy-MM-dd转为long格式的yyyyMMdd
	 * @return
	 */
	public String getTime(String time){
		return DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(time, DateUtil.PATTERN_yyyy_MM_dd), DateUtil.PATTERN_yyyyMMdd);
	}
	
	public List<String> getRandomData(){
		List<String> value=FrameUtil.newArrayList();
		for(int i=0;i<3;i++){
			 Random random = new Random();
			value.add(random.nextInt(10)+"");
		}
		return value;
	}
	/**
	 * 获取初始数据
	 * @param timeType
	 * @param timeFrom
	 * @throws Exception
	 */
	@RequestMapping("getDatasByField")
	public void getDatas(String timeType,String timeFrom,String field) throws Exception{
		try{
			timeFrom=DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(timeFrom, DateUtil.PATTERN_yyyy_MM_dd),"yyyyMMdd");
			TimeType.getType(Integer.parseInt(timeType));
			Long.parseLong(timeFrom);
		}catch(Exception e){
			this.errorMsg("参数有误！");
		}
		TimeType t=TimeType.getType(Integer.parseInt(timeType));
		EChartsModel ec=this.biFundsStatisticsService.getDates(t, Long.parseLong(timeFrom), field);
		this.successData(FrameUtil.newHashMap("option",ec));
	}
	
	/**
	 * 根据日期获取时段诗句
	 * @param timeType
	 * @param timeFrom
	 * @throws Exception
	 */
	@RequestMapping("getDailyDatasByField")
	public void getDailyDatas(String timeFrom,String field) throws Exception{
		try{
			timeFrom=DateUtil.timestampToDateString(DateUtil.dateStringToTimestamp(timeFrom, DateUtil.PATTERN_yyyy_MM_dd),"yyyyMMddHHmmss");
		}catch(Exception e){
			this.errorMsg("参数有误！");
		}
		EChartsModel ec=this.biFundsStatisticsMinuteService.getDates(Long.parseLong(timeFrom), field);
		this.successData(FrameUtil.newHashMap("option",ec));
	}
	
}
